package com.skp.shaphan.toolbars;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;

import com.skp.shaphan.ShaphanApp;
import com.skp.shaphan.actions.ExecuteSQLAction;
import com.skp.shaphan.ui.SQLEditor;

public class RunButton extends JButton implements ActionListener {
	private ExecuteSQLAction action;
	private SQLEditor associatedEditor;

	public RunButton(SQLEditor associatedEditor) {
		super("Run");
		this.setMnemonic('R');
		this.addActionListener(this);
		action = null;
		this.associatedEditor = associatedEditor;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(action != null) {
			System.out.println("Thread state = " + action.getState().ordinal() + " -- " + action.getState().name());
		}
		action = new ExecuteSQLAction();
		if(associatedEditor == null) {
			action.setEditor(ShaphanApp.getMainWindow().getSqlEditor());
		} else {
			action.setEditor(associatedEditor);
		}
		action.setResultPane(ShaphanApp.getMainWindow().getResultPane());
		action.start();
	}

}
